﻿using System;
using System.Data;
using System.IO;
using System.Net.Mail;
using System.Web;

namespace MasApp.BLL
{
    public class ResponseHelper
    {
        public ResponseHelper()
        {

        }
        public static void SendMail(Model.Email em)
        {
            string senderName = BLL.Search.SearchUserRealName(HttpContext.Current.Session["username"].ToString());
            MailMessage mm = new MailMessage();
            mm.Sender = new MailAddress(BLL.Search.SearchEmailAddr(), senderName);
            mm.ReplyTo = new MailAddress(em.To, senderName);
            mm.Subject = "邀请信";
            mm.Body = "您好，您被我网站的用户“"
                + senderName + "”邀请成为我们的【"
                + em.Rolename + "】用户，请记住您的用户名与密码：" + em.Username + "； 密码：" + em.Password
                + "。<br />如果您知道该用户的事宜或者该用户的组织相关信息，那么很抱歉，打搅您了。";
            mm.BodyEncoding = System.Text.Encoding.UTF8;
            SmtpClient sc = new SmtpClient("127.0.0.1");
        }
        /// <summary>
        /// 还原数据库
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool RestoreDatabase(string path)
        {
            if (Directory.Exists(path))
            {
                MasApp.DbUtil.Db.BackUpDatabase("CREATE DATABASE IF NOT EXISTS app");
                string s = "Restore Database app "
                    + " From disk='" + path + "'";
                MasApp.DbUtil.Db.BackUpDatabase(s);
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 备份数据库
        /// </summary>
        /// <param name="databaseName"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool BackUpDatabase(string databaseName, string path)
        {
            if (Directory.Exists(path))
            {
                string s = "BACKUP DATABASE app TO DISK='" + path + databaseName + "'";
                MasApp.DbUtil.Db.BackUpDatabase(s);
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url">转向的URL</param>
        public static void Redirect(string url)
        {
            System.Web.HttpContext.Current.Response.Write("<script language='javascript'"
                + " type='text/javascript'>location.href='" + url + "';</script>");
        }
        /// <summary>
        /// <script language='javascript' type='text/javascript'></script>
        /// </summary>
        /// <param name="url">转向的URL</param>
        /// <param name="msg">警告信息</param>
        public static void alert(string msg, string url)
        {
            System.Web.HttpContext.Current.Response.Write("<script language='javascript'"
                + " type='text/javascript'>window.alert('" + msg + "');"
                + " location.href='" + url + "';</script>");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg">警告信息</param>
        public static void alert(string msg)
        {
            System.Web.HttpContext.Current.Response.Write("<script language='javascript'"
                + " type='text/javascript'>window.alert('" + msg + "');</script>");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectid"></param>
        public static void WriteDirectWeight(int projectid)
        {
            int modelid = MasApp.BLL.Search.SearchModelID(projectid);
            int methodid = MasApp.BLL.Search.SearchMethodID(projectid);
            DataTable dt_rule = MasApp.BLL.Search.SearchRuleIDsandNames(modelid);

            HttpContext.Current.Response.Write("<div class='target'></div>");
            for (int i = 0; i < dt_rule.Rows.Count; i++)
            {
                DataTable dt_indi = MasApp.BLL.Search.SearchIndicatorIDsandNames(Convert.ToInt32(dt_rule.Rows[i][0].ToString()));
                HttpContext.Current.Response.Write("<div class='rule'>");
                HttpContext.Current.Response.Write("----准则“" + dt_rule.Rows[i][1].ToString() + "”的权重：");
                HttpContext.Current.Response.Write("<input id='rule" + (i + 1).ToString() + "' name='rule" + (i + 1).ToString() + "' type='text' />");
                HttpContext.Current.Response.Write("</div>");
                for (int j = 0; j < dt_indi.Rows.Count; j++)
                {
                    HttpContext.Current.Response.Write("<div class='indi'>");
                    HttpContext.Current.Response.Write("--------指标：" + dt_indi.Rows[j][1].ToString());
                    HttpContext.Current.Response.Write("<input id='indi" + (i + 1).ToString() + (j + 1).ToString() + "' name='indi" + (i + 1).ToString() + (j + 1).ToString() + "' type='text' />");
                    HttpContext.Current.Response.Write("</div>");
                }
            }
        }
    }
}
